# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  
  #显式页面顶端的登录用户信息
  def admin_info
    if session[:user]!=nil
      " 当前登录用户: #{current_user.name} | 
        <a href='/admin/album'>管理</a> |  
        <a href='/account/logout'>注销</a>"
    else
      "<a href='/account/login'>登录</a>"
    end
  end
  
  # menu uses better nested set
  # menuselect is the id of currently selected menu
  def showmenu(menuselect)
    if menuselect
      @menuselect = Menu.find(menuselect.to_i)
      selectpath = @menuselect.self_and_ancestors
    else
      @menuselect = nil
      selectpath = []
    end
    parents = (selectpath.map{|m| m.parent}+[@menuselect]).uniq-[nil]
    parents_sql_filter = parents.empty? ? '' : 
    " OR parent_id IN (#{parents.map{|p| p.id}.join(',')})"
  
    # retrieve only the menus that need to be open
    allmenus = Menu.find(:all, 
      :conditions => "(parent_id IS NULL #{parents_sql_filter})", :order => 'lft')
    # mark where to open and close html lists
    @menus = []
    allmenus.each_index { |i|
      @menus[i] = {:indent => allmenus[i].level, 
        :title  => allmenus[i].title,
        :children_count => allmenus[i].all_children_count,
        :url  =>  allmenus[i].page ? allmenus[i].page.whole_url : ''}
      @menus[i][:selected] =  allmenus[i] == @menuselect
    }
    allmenus[1..-1].each_index { |i|
      @menus[i][:open] = @menus[i][:indent] > @menus[i-1][:indent]
      @menus[i][:close] = [0, @menus[i][:indent] - @menus[i+1][:indent]].max
    }
    @menus.first[:open] = true
    @menus.last[:open] = false
    @menus.first[:close] = 0
    @menus.last[:close] = 1
    
    render 'menu/showmenu'
  end 
  
  def category_name(blog)
    blog.category ? blog.category.name : "默认类别"
  end  
   
  def category_link(blog)
    category_id=blog.category ? blog.category.id : nil
    {:controller=>:blog, :action=>"list_in_category", :category_id=>category_id}  
  end  
  
  def photo_link(photo,album,url)
    if photo==nil
      link_to(
        image_tag("default_cover.jpg",:alt => "默认封面"),  url, 
        :title =>album.name)              
    else
      link_to(
        "#{image_tag url_for_file_column("photo", "image","thumb")}",  url, 
        :title =>album.name)
    end
  end
  
  def photo_title(photo)
    value=photo.title==nil ? sanitize_filename(photo.image) : photo.title
    return truncate(value,18) 
  end
  
  def sanitize_filename(value)
    # get only the filename, not the whole path
    just_filename = value.gsub(/^.*(\\|\/)/, '')
    # NOTE: File.basename doesn't work right with Windows paths on Unix
    # INCORRECT: just_filename = File.basename(value.gsub('\\\\', '/')) 

    # Finally, replace all non alphanumeric, underscore or periods with underscore
    @filename = just_filename.gsub(/[^\w\.\-]/,'_') 
  end
  
  def photo_admin_url(photo)
    if logged_in?
      #      value=link_to "编辑", 
      #        {:controller=>'admin/album', :action=>:edit_photo, :id=>photo}
      #      value+=' | '
      value=link_to "删除", 
        {:controller=>'admin/album', :action=>:delete_photo, :id=>photo},
        :confirm=>'删除该相片以及它的全部评论？'
    end
    return value
  end
  
  #show url to list photos in a album
  def album_info_url(album,short)
    url=logged_in? ? '/admin/album' : '/album'
    info=short ? truncate(album.name, 10) : album.name
    info+="(#{album.photos.size})"
    link_to info,:controller=>url,:action=>'list_photos',:id=>album
  end
  
  #show or not show album admin url based on whether the user logged in
  def album_admin_url(album)
    if logged_in?
      value=link_to "管理|", 
        {:controller=>'admin/album', :action=>:view_album, :id=>album}
      value+=link_to "编辑|", 
        {:controller=>'admin/album', :action=>:edit_album, :id=>album}
      value+=link_to "删除", 
        {:controller=>'admin/album', :action=>:delete_album, :id=>album},
        :confirm=>'删除该相册以及它的全部相片？'
    end
    return value
  end
  

  #make a album div use highslide js
  def album_cover_div(album)
    @cover_photo=album.cover_photo
    if @cover_photo==nil
      thumb="/images/default_cover.jpg"   
      normal="/images/default_cover.jpg"   
    else
      thumb=url_for_file_column('cover_photo', 'image','thumb')
      normal=url_for_file_column('cover_photo', 'image','normal')
    end
    div_content="<a id='thumb_#{album.id}' "
    div_content+="href='#{normal}' "
    div_content+="class='highslide' " 
    div_content+="onclick=\"return hs.expand(this, {captionId: 'caption_#{album.id}'})\">"
    div_content+="<img src=#{thumb} "
    div_content+="alt='' title='点击放大'/> </a>"
    div_content+="<div class='highslide-caption' id='caption_#{album.id}'>"
    div_content+="#{album_info_url(album,false)}"
    div_content+="</div>"
    return div_content
  end
  
  #make a highslide js css style photo div
  def highslide_photo(photo)
    thumb=url_for_file_column('photo', 'image','thumb')
    normal=url_for_file_column('photo', 'image','normal')
    div_content="<a id='thumb_#{photo.id}' "
    div_content+="href='#{normal}' "
    div_content+="class='highslide' " 
    div_content+="onclick=\"return hs.expand(this, {captionId: 'caption_#{photo.id}'})\">"
    div_content+="<img src=#{thumb} "
    div_content+="alt='' title='点击放大'/> </a>"
    div_content+="<div class='highslide-caption' id='caption_#{photo.id}'>"
    div_content+="#{photo.title} <br> "
    div_content+="评论:(#{photo.comments.size}) "
    div_content+=link_to '我来说两句...', :action=>'view_photo', :id=>photo
    div_content+="</div>"
    return div_content
  end
  
  #为图片产生阴影特效
  def photo_div_with_shadow
    div_content=<<-CONTENT
      <div class="shadow">
        <div>
          <a>
    CONTENT
    div_content+= image_tag url_for_file_column("photo", "image","normal")
    div_content+=<<-CONTENT
          </a>
      </div>
      </div>
    CONTENT
    return div_content
  end
  
  #相册导航按钮
  def button_to_next(photo)
    button_to("<---" , 
      {:action=>"view_photo",:id=>photo.previous_in(photo.album).id}) if 
    photo.previous_in(photo.album)
  end
  
  def button_to_previous(photo)
    button_to("--->" , 
      {:action=>"view_photo",:id=>photo.next_in(photo.album).id}) if 
    photo.next_in(photo.album)
  end
  
  #根据不同的情况设置显式的album side bar
  def album_sidebar(type)
    case type
    when :album_list
      value=<<-CONTENT
       <h2><b>最新评论</b></h2>
        <ul>
      CONTENT
      @recent_comments.each do |comment|
        value+='<li>'
        value+=link_to truncate(comment.content,10), 
          :controller=>'album', 
          :action=>'view_photo', 
          :id=>comment.resource
        value+='</li>'
      end
      value+='</ul>'
    when :photo_list
      value="<h2><b>相册信息</b></h2>
        <ul>
        <li>
        相册名称:#{@album.name}
        </li>
        </ul>"
      
    when :photo_view
      value="
        <h2><b>图片信息</b></h2>
        <ul>
        <li>
        图片名称:#{photo_title(@photo)}
        </li>
        <li>
        所属相册:#{link_to @photo.album.name, 
      :action=>'list_photos', 
      :id=>@photo.album}
          </li>
        </ul>"
    end
    return value
  end
  
end
